<template>
  <div class="app-container">
    <eHeader :query="query" />
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
      <el-table-column prop="planName" :show-overflow-tooltip="true" label="计划名称" />
      <el-table-column prop="startTime" :show-overflow-tooltip="true" label="开始时间">
        <template slot-scope="scope">
          <span>{{ parseTimeShort(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="endTime" :show-overflow-tooltip="true" label="结束时间">
        <template slot-scope="scope">
          <span>{{ parseTimeShort(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="periodName" :show-overflow-tooltip="true" label="巡检周期" />

      <el-table-column prop="vehTypeName" :show-overflow-tooltip="true" label="巡检对象" />
      <el-table-column prop="templateName" :show-overflow-tooltip="true" label="巡检内容" />
      <el-table-column prop="inspectorName" :show-overflow-tooltip="true" label="服务商" />
      <el-table-column prop="planDesc" :show-overflow-tooltip="true" label="巡检要求" />
      <el-table-column prop="state" :show-overflow-tooltip="true" label="计划状态">
        <template slot-scope="scope">
          <span>{{ scope.row.state === 1 ? "启动" : "暂定" }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['ADMIN','INSPECTIONPLAN_ALL','INSPECTIONPLAN_EDIT','INSPECTIONPLAN_DELETE'])" label="操作" width="180px" align="center">
        <template slot-scope="scope">
          <edit :data="scope.row" :is-add="false" :is-view="true" :sup-this="supThis" />
          <edit v-permission="['ADMIN','INSPECTIONPLAN_ALL','INSPECTIONPLAN_EDIT']" :data="scope.row" :is-view="false" :is-add="false" :sup-this="supThis" />
          <el-popover
            :ref="scope.row.id"
            v-permission="['ADMIN','INSPECTIONPLAN_ALL','INSPECTIONPLAN_DELETE']"
            placement="top"
            width="180"
          >
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini" />
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { del } from '@/api/inspectionPlan'
import { parseTime, parseTimeShort } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
export default {
  components: { eHeader, edit },
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      supThis: this
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    parseTimeShort,
    checkPermission,
    beforeInit() {
      this.url = 'api/inspectionPlan'
      const sort = 'id,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      if (query.planName) {
        this.params['planName'] = query.planName
      }
      if (query.objid) {
        this.params['objId'] = query.objid
      }
      if (query.templateId) {
        this.params['templateId'] = query.templateId
      }
      if (parseInt(query.state) >= 0) {
        this.params['state'] = query.state
      }
      return true
    },
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    }
  }
}
</script>

<style scoped>

</style>
